package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {

    StudentDao studentDao = new StudentDao();

    //将库管的学生数组返回给业务员
    public Student[] findAllStudent() {
        Student[] stus = studentDao.findAllStudent();
        boolean flag = false;
        //判断数组中是否有学生信息
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            //判断只要不为空就表示有学生信息
            if (student != null) {
                flag = true;
                break;
            }
        }
        //判断若为真就表示有信息，若为假就表示没信息
        if (flag) {
            return stus;
        } else {
            return null;
        }
    }

    //经过业务员将学生的信息传递给库管，并且将返回值返回给controller
    public boolean addStudent(Student stu) {

        return studentDao.addStudent(stu);


    }

    //是否存在的方法
    public boolean isExists(String id) {
        //调用Dao中的findAllStudent并将数组返回到这里
        Student[] stus = studentDao.findAllStudent();
        //假设ID在数组中不存在
        boolean exists = false;
        //遍历数组，获取每一个对象进行判断
        for (int i = 0; i < stus.length; i++) {
            Student student = stus[i];
            if (student != null && student.getId().equals(id)) {
                exists = true;
            }
            break;
        }
        return exists;
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }

    public void updateStudentById(String updateId, Student newStu) {
        studentDao.updateStudentById(updateId, newStu);
    }
}
